﻿// readAp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <fstream>
#include <string>
#include "readAp.h"

using namespace std;

void getApLine(DateTime& t, double ap[9])
{
	ifstream apfile;
	string filename = to_string(t.Year());
	apfile.open(filename);
	if (!apfile)
	{
		cout << "the file needed is not present" << filename << endl;
		exit(1);
	}
	//while (apfile >> line)
	//{
	streamoff offset = lineLength * (t.DayOfYear(t.Year(), t.Month(), t.Day())-1);
	apfile.seekg(offset, ios::beg);
	char tmpline[lineLength];
	apfile.getline(tmpline,lineLength);

	string line, tmp1;
	int tmpMonth, tmpDay;
	line = tmpline;
	tmp1 = line.substr(2, 2);
	tmpMonth = stoi(tmp1);
	tmp1 = line.substr(4, 2);
	tmpDay = stoi(tmp1);
	if (tmpMonth == t.Month() && tmpDay == t.Day())
	{
		//find ap data for current day
		//
		for (int i = 1; i < 10; i++)
		{
			tmp1 = line.substr(apStartPos + (i - 1) * 3, 3);
			ap[i - 1] = stod(tmp1);
		}
		//break;
	}
	//}
	apfile.close();
}

double getDailyAp(int year, int month, int day)
{
	double dailyAp = -1.0;
	ifstream apfile;
	string filename;

	if (year >= minApYear && year <= maxApYear)
		filename = to_string(year);
	else
	{
		cout << "no data for " << year << endl;
		return -1.0;
	}
	apfile.open(filename);
	string line, tmp1;
	int tmpMonth, tmpDay;
	do
	{
		apfile >> line;
		tmp1 = line.substr(2, 3);
		tmpMonth = stoi(tmp1);
		tmp1 = line.substr(4, 5);
		tmpDay = stoi(tmp1);
		if (tmpDay == day)
		{
			tmp1 = line.substr(55, 57);
			dailyAp = stod(tmp1);
			break;
		}
	} while (!apfile.eof());
	return dailyAp;
}

//todo:xxxx-1-2怎么处理
void getApArray(DateTime givenDT, double *outApData)
{
	//every 3 hours - one ap value, and daily averaged ap, 9 element in a row
	//for three days, 3 rows
	//current day - aparray[2][0]-[2][8], yesterday:[1][0]-[1][8], the day before yesterday:[0][0]-[0][8]
	double aparray[3][9];
	ifstream apfile;
	string filename;
	DateTime givenDTCopy(givenDT);

	if (givenDTCopy.Year() < minApYear || givenDTCopy.Year() > maxApYear)
	{
		cout << "no data for " << givenDTCopy.Year() << endl;
		exit(1);
	}
	else
	{
		getApLine(givenDTCopy, &(aparray[2][0]));
		givenDTCopy = givenDTCopy.AddDays(-1.0);
		getApLine(givenDTCopy, &(aparray[1][0]));
		givenDTCopy = givenDTCopy.AddDays(-1.0);
		getApLine(givenDTCopy, &(aparray[0][0]));
		//output ap data
		//treat aparray like a one-dimentional array
		outApData[0] = aparray[2][8]; //daily ap
		int hourIndex = givenDTCopy.Hour() / 3;
		outApData[1] = aparray[2][hourIndex];
		outApData[2] = *(&(aparray[0][0]) + 19 + hourIndex - 1); //3 hours before
		outApData[3] = *(&(aparray[0][0]) + 19 + hourIndex - 2); //6 hours before
		outApData[4] = *(&(aparray[0][0]) + 19 + hourIndex - 3); //9 hours before

		//12-33 hours before averaged
		double avg1 = 0.0;
		for (int i = 0; i < 8; i++)
		{
			avg1 = avg1 + *(&(aparray[0][0]) + 19 + hourIndex - 4 - i);
		}
		outApData[5] = avg1 / 8.0;
		int tmp = outApData[5] * 10.0 + 4.99;
		tmp = tmp / 10;
		outApData[5] = tmp;

		//36-57 hours before averaged
		avg1 = 0.0;
		for (int i = 0; i < 8; i++)
		{
			avg1 = avg1 + *(&(aparray[0][0]) + 19 + hourIndex - 12 - i);
		}
		outApData[6] = avg1 / 8.0;
		tmp = outApData[6] * 10.0 + 4.99;
		tmp = tmp / 10;
		outApData[6] = tmp;
	}
}